package dream_scene

import (
	"go_server/game_server/api/service/bag"
	"go_server/game_server/dao/scene_info_dao"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/protocol/pb"
	"go_server/game_server/util"
	"go_server/game_server/world_manager"
	"go_server/zinx/ziface"
	"go_server/zinx/zlog"
	"go_server/zinx/znet"
	"gorm.io/datatypes"
)

type GetDreamSceneBoxPrizeApi struct {
	znet.BaseRouter
}

func (*GetDreamSceneBoxPrizeApi) Handle(request ziface.IRequest) {
	msg := &pb.GetDreamSceneBoxPrizeC2S{}
	if !util.PBParseRet(request, msg) {
		return
	}

	conn := request.GetConnection()
	roleId := ServiceObj.GetRoleId(request)

	if msg.SceneId <= 0 || msg.Index <= 0 || msg.Index > 3 {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_PARAM_ERROR, nil)
		return
	}

	index := msg.Index - 1 // 领取宝箱下标

	baseSceneInfo, ok := config_manager.GetStcTableLevelChapter_SheetLevelChapterById(msg.SceneId)
	if !ok || baseSceneInfo.EntType != DREAM_SCENE_EVENT_TYPE {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_DREARM_SCENE_UNFIND, nil)
		return
	}

	row := scene_info_dao.GetSceneByRoleId(roleId, []string{"scene_info", "box_info"})
	if row == nil {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_DREARM_SCENE_UNINIT, nil)
		return
	}

	tmpSceneMap := make(map[int32][]int32)
	if row.SceneInfo.Data() != nil {
		tmpSceneMap = row.SceneInfo.Data()
	}

	tmpBoxMap := make(map[int32][]int32)
	if row.BoxInfo.Data() != nil {
		tmpBoxMap = row.BoxInfo.Data()
	}

	sceneInfo, ok := tmpSceneMap[msg.SceneId]
	if !ok {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_DREARM_SCENE_UNLOCK, nil)
		return
	}

	totalStar := sceneInfo[2] // 当前关卡累计星级

	boxInfo, ok := tmpBoxMap[msg.SceneId]
	if !ok {
		boxInfo = []int32{0, 0, 0}
	}

	limitStar := baseSceneInfo.StarNum[index] // 限制星级
	if boxInfo[index] >= 2 {
		zlog.Errorf("梦境关卡进度宝箱已领取")
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_DREARM_SCENE_BOX_HAD_GOT, nil)
		return
	}

	if totalStar < limitStar {
		zlog.Errorf("星级不达标,无法领取梦境关卡进度宝箱")
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_DREARM_SCENE_BOX_UNCOMPLETE, nil)
		return
	}

	boxInfo[index] = 2
	tmpBoxMap[msg.SceneId] = boxInfo

	// 组装返回数据结构
	res := new(pb.GetDreamSceneBoxPrizeS2C)
	res.SceneId = msg.SceneId
	res.BoxStates = boxInfo

	for indexKey, limitStarValue := range baseSceneInfo.StarNum {
		if boxInfo[indexKey] < 2 && totalStar >= limitStarValue {
			boxInfo[indexKey] = 1 // 可领取
		}
	}

	conn.SendBuffPBMsg(uint32(pb.ProtocolManager_GetDreamSceneBoxPrize), res)

	myMap := map[string]any{
		"box_info": datatypes.NewJSONType(tmpBoxMap),
	}
	scene_info_dao.UpdateSceneByRoleId(roleId, &myMap) // 修改进度宝箱状态状态

	prizeMap := make(map[int32]int32) // 宝箱奖励

	switch index {
	case 0:
		{
			if baseSceneInfo.StarReward1 != nil {
				for _, v := range baseSceneInfo.StarReward1 {
					prizeMap[v.Value[0]] = v.Value[1]
				}
			}
		}
	case 1:
		{
			if baseSceneInfo.StarReward2 != nil {
				for _, v := range baseSceneInfo.StarReward2 {
					prizeMap[v.Value[0]] = v.Value[1]
				}
			}
		}
	case 2:
		{
			if baseSceneInfo.StarReward3 != nil {
				for _, v := range baseSceneInfo.StarReward3 {
					prizeMap[v.Value[0]] = v.Value[1]
				}
			}
		}
	}

	if len(prizeMap) > 0 {
		bag.AddItemBatch(roleId, prizeMap) // 宝箱奖励
	}
}
